package android.slc.icr.service.impl;


import android.slc.icr.core.IIcrManager;
import android.slc.icr.core.IcrManager;
import android.slc.icr.core.constant.IcrConstants;
import android.slc.icr.core.entity.IcrChartRoom;
import android.slc.icr.core.entity.IcrMessage;
import android.slc.icr.core.room.manager.IChartRoomManager;
import android.slc.icr.core.room.manager.LiveChartRoomManager;
import android.slc.icr.core.room.manager.StandardChartRoomManager;
import android.slc.icr.service.IcrManagerService;
import android.slc.icr.service.IcrMessageRemoteService;
import android.slc.icr.service.IcrPersistenceService;

import java.util.List;

/**
 * 此类就是一个桥接类，目的之为了避免直接操作(@{@link IcrManager})
 */
public class IcrManagerServiceImpl implements IcrManagerService {

    protected final IIcrManager webRtcManager = IcrManager.getInstance();

    @Override
    public void init(IcrMessageRemoteService icrMessageRemoteService, IcrPersistenceService icrPersistenceService) {
        webRtcManager.init(icrMessageRemoteService, icrPersistenceService);
        registerRoomManager(StandardChartRoomManager.getInstance());
        registerRoomManager(LiveChartRoomManager.getInstance());
    }

    @Override
    public void registerRoomManager(IChartRoomManager roomManager) {
        webRtcManager.registerRoomManager(roomManager);
    }

    @Override
    public <T extends IChartRoomManager> T findRoomManager(String roomManagerType) {
        return webRtcManager.findRoomManager(roomManagerType);
    }

    @Override
    public void receiveMessage(IcrMessage message) {
        webRtcManager.receiveMessage(message);
    }

    @Override
    public List<IcrChartRoom> getChartRoomList(String msgType) {
        return webRtcManager.getChartRoomList(msgType);
    }

    @Override
    public List<IcrChartRoom> getChartRoomListByCreateUser(String msgType, String createUser) {
        return webRtcManager.getChartRoomListByCreateUser(msgType, createUser);
    }

    @Override
    public IcrChartRoom findChartRoomByRoomId(String roomId, String msgType) {
        return webRtcManager.findChartRoomByRoomId(roomId, msgType);
    }

    /*@Override
    public void onMessage(Map<String, String> data) {
        String extra = data.get("extra");
        IcrMessageExtra icrMessageExtra = JSONUtil.toBean(extra, IcrMessageExtra.class);
        IcrMessage icrMessage = JSONUtil.toBean(JSONUtil.toJsonStr(data), IcrMessage.class);
        icrMessage.setRelate(icrMessageExtra.getRelate());
        icrMessage.setRelateId(icrMessageExtra.getRelateId());
        webRtcManager.receiveMessage(icrMessage);
    }*/

    @Override
    public void onMessage(IcrMessage icrMessage) {
        webRtcManager.receiveMessage(icrMessage);
    }

    @Override
    public void onDestroyMessage(String chartRoomId, String userId, String chartRoomType) {
        IcrMessage icrMessage = IcrMessage.newBuilder()
                .setSender(userId)
                .setReceiver(chartRoomId)
                .setRelateId(chartRoomId)
                .setRelate(chartRoomType)
                .setAction(IcrConstants.Action.ACTION_CHART_ROOM_DESTROY)
                .setTitle(IcrConstants.Action.ACTION_CHART_ROOM_DESTROY)
                .build();
        webRtcManager.receiveMessage(icrMessage);
    }

    @Override
    public void userImDisconnected(String userId) {
        //用户掉线
        receiveMessage(IcrMessage.newBuilder().setSender(userId).setReceiver(userId).setAction(IcrConstants.Action.ACTION_CHART_ROOM_LEFT).build());
    }

}
